home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / imagetoo / imagetl1.lha / Imagetool / src+obj / plot2d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  7.3 KB  |  337 lines

  1. /* cat > headers/plot2d.h << "EOF" */
  2. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  3. /* plot2d.h: header for plot2d.c file            */
  4. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  5. /* SCCS information: %W%    %G% - NCSA */
  6.  
  7. #define    plot2d_h    1
  8.  
  9. #include "all.h"
  10. #include "newext.h"
  11.  
  12. /*
  13.     static int c_levels = 11;
  14.     static int c_array[] = { 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240 };
  15. */
  16.  
  17. /* EOF */
  18. /* cat > src+obj/plot2d/contour.c << "EOF" */
  19. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  20. /* contour: contour plotting                */
  21. /*        int nc  -- number of contour levels        */
  22. /*        int z[] -- "nc" contour levels in        */
  23. /*                   increasing order            */
  24. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  25. /* SCCS information: %W%    %G% - NCSA */
  26.  
  27. /* #include "plot2d.h" */
  28.  
  29. void
  30. contour (datx, daty, datap, nc, z)
  31.     int             datx, daty, nc, z[];
  32. Point          *datap;
  33.  
  34. {
  35.     int             i, j, k, jump, dmin, dmax;
  36.     int             h0, h1, h2, h3;
  37.     int             d0, d1, d2, d3;
  38.     float           x1, y1, x2, y2;
  39.     float           xc, yc, xn, yn;
  40.     unsigned char   b[CANVAS_XSIZ];
  41.     Point         **data, pp, pv;
  42.  
  43.         /* Check the input parameters for validity */
  44.     if (datx <= 0 || daty <= 0 || nc <= 0)
  45.         return;
  46.     for (k = 1; k < nc; k++)
  47.         if (z[k] <= z[k - 1])
  48.             return;
  49.  
  50.         /* Dynamically allocate spaces */
  51.     data = (Point **) malloc (daty * sizeof (Point *));
  52.     if (data == NULL)
  53.     {
  54.         msg_write ("Error: Contour memory allocation failed.");
  55.         return;
  56.     }
  57.     data[0] = datap;
  58.     for (j = 1; j < daty; j++)
  59.     {
  60.         data[j] = data[j - 1] + datx;
  61.     }
  62.  
  63.     pp = data[0][0];
  64.     pv = data[daty - 1][datx - 1];
  65.     gfx_init ();
  66.     hid_scale (pp.x, pv.x, pp.y, pv.y);
  67.     win_scale (pp.x, pv.x, pp.y, pv.y);
  68.  
  69.     gfx_move (pp.x, pp.y);
  70.     gfx_vector (pv.x, pp.y, 0);
  71.     gfx_vector (pv.x, pv.y, 0);
  72.     gfx_vector (pp.x, pv.y, 0);
  73.     gfx_vector (pp.x, pp.y, 0);
  74.  
  75.         /* Scan the array, top down, left to right */
  76.     daty--;
  77.     datx--;
  78.     for (j = 0; j < daty; j++)
  79.     {
  80.         for (i = 0; i < datx; i++)
  81.         {
  82.  
  83.             d0 = data[j][i].z;
  84.             d1 = data[j][i + 1].z;
  85.             d2 = data[j + 1][i].z;
  86.             d3 = data[j + 1][i + 1].z;
  87.  
  88.             xc = data[j][i].x;
  89.             xn = data[j][i + 1].x;
  90.             yc = data[j][i].y;
  91.             yn = data[j + 1][i].y;
  92.  
  93.             dmax = dmin = d0;
  94.             if (d1 < dmin)
  95.                 dmin = d1;
  96.             if (d2 < dmin)
  97.                 dmin = d2;
  98.             if (d3 < dmin)
  99.                 dmin = d3;
  100.  
  101.             if (d1 > dmax)
  102.                 dmax = d1;
  103.             if (d2 > dmax)
  104.                 dmax = d2;
  105.             if (d3 > dmax)
  106.                 dmax = d3;
  107.  
  108.             if (dmax < z[0] || dmin > z[nc - 1] || dmin == dmax)
  109.                 continue;
  110.  
  111.             for (k = 0; k < nc; k++)
  112.             {
  113.                 if (z[k] < dmin || z[k] > dmax)
  114.                     continue;
  115.  
  116.                 h0 = d0 - z[k];
  117.                 h1 = d1 - z[k];
  118.                 h2 = d2 - z[k];
  119.                 h3 = d3 - z[k];
  120.  
  121.                 jump = 0;
  122.                 jump |= (h0 >= 0) << 3;
  123.                 jump |= (h1 >= 0) << 2;
  124.                 jump |= (h2 >= 0) << 1;
  125.                 jump |= (h3 >= 0);
  126.  
  127.                 switch (jump)
  128.                 {
  129.                     case 0:
  130.                     case 15:
  131.                         continue;
  132.                     case 1:    /* Line between sides 1-3 and 2-3 */
  133.                     case 14:
  134.                         if (!h3)
  135.                         {
  136.                             b[i] = 2;
  137.                             continue;
  138.                         }
  139.                         x1 = xn;
  140.                         y1 = (h3 * yc - h1 * yn) / (h3 - h1);
  141.                         x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  142.                         y2 = yn;
  143.                         break;
  144.                     case 2:    /* Line between sides 0-2 and 2-3 */
  145.                     case 13:
  146.                         if (!h2)
  147.                         {
  148.                             b[i] = 2;
  149.                             continue;
  150.                         }
  151.                         x1 = xc;
  152.                         y1 = (h2 * yc - h0 * yn) / (h2 - h0);
  153.                         x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  154.                         y2 = yn;
  155.                         break;
  156.                     case 3:    /* Line between sides 0-2 and 1-3 */
  157.                     case 12:
  158.                         x1 = xc;
  159.                         y1 = (h2 * yc - h0 * yn) / (h2 - h0);
  160.                         x2 = xn;
  161.                         y2 = (h3 * yc - h1 * yn) / (h3 - h1);
  162.                         break;
  163.                     case 4:    /* Line between sides 0-1 and 1-3 */
  164.                     case 11:
  165.                         if (!h1)
  166.                             continue;
  167.                         x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  168.                         y1 = yc;
  169.                         x2 = xn;
  170.                         y2 = (h3 * yc - h1 * yn) / (h3 - h1);
  171.                         break;
  172.                     case 5:    /* Line between sides 0-1 and 2-3 */
  173.                     case 10:
  174.                         x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  175.                         y1 = yc;
  176.                         x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  177.                         y2 = yn;
  178.                         break;
  179.                     case 6:
  180.                     case 9:
  181.                         if (b[i] == 0)
  182.                         {    /* Lines 0-1 to 1-3 and 0-2 to 2-3 */
  183.                             x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  184.                             y1 = yc;
  185.                             x2 = xn;
  186.                             y2 = (h3 * yc - h1 * yn) / (h3 - h1);
  187.                             gfx_line (x1, y1, x2, y2, 0);
  188.                             x1 = xc;
  189.                             y1 = (h2 * yc - h0 * yn) / (h2 - h0);
  190.                             x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  191.                             y2 = yn;
  192.                             break;
  193.                         }
  194.                         if (b[i] == 1)
  195.                         {    /* Lines 0-1 to 0-2 and 1-3 to 2-3 */
  196.                             x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  197.                             y1 = yc;
  198.                             x2 = xc;
  199.                             y2 = (h2 * yc - h0 * yn) / (h2 - h0);
  200.                             gfx_line (x1, y1, x2, y2, 0);
  201.                             x1 = xn;
  202.                             y1 = (h3 * yc - h1 * yn) / (h3 - h1);
  203.                             x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  204.                             y2 = yn;
  205.                             break;
  206.                         }
  207.                             /* Line between sides 0-2 and 1-3 */
  208.                         x1 = xc;
  209.                         y1 = (h2 * yc - h0 * yn) / (h2 - h0);
  210.                         x2 = xn;
  211.                         y2 = (h3 * yc - h1 * yn) / (h3 - h1);
  212.                         gfx_line (x1, y1, x2, y2, 0);
  213.                             /* Line between sides 0-1 and 2-3 */
  214.                         x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  215.                         y1 = yc;
  216.                         x2 = (h3 * xc - h2 * xn) / (h3 - h2);
  217.                         y2 = yn;
  218.                         break;
  219.                     case 7:    /* Line between sides 0-1 and 0-2 */
  220.                     case 8:
  221.                         if (!h0)
  222.                             continue;
  223.                         x1 = (h1 * xc - h0 * xn) / (h1 - h0);
  224.                         y1 = yc;
  225.                         x2 = xc;
  226.                         y2 = (h2 * yc - h0 * yn) / (h2 - h0);
  227.                         break;
  228.                     default:
  229.                         continue;
  230.                 }
  231.                 gfx_line (x1, y1, x2, y2, 0);
  232.  
  233.                 if (x1 > x2)
  234.                     b[i] = 1;
  235.                 else if (x1 < x2)
  236.                     b[i] = 0;
  237.                 else
  238.                     b[i] = 2;
  239.  
  240.             }    /* end of k loop */
  241.         }        /* end of i loop */
  242.     }            /* end of j loop */
  243.  
  244.     free (data);
  245. }
  246. /* EOF */
  247. /* cat > src+obj/plot2d/plot_twod.c << "EOF" */
  248. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  249. /* plot_twod: 2-D plotting procedure            */
  250. /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  251. /* SCCS information: %W%    %G% - NCSA */
  252.  
  253. /* #include "plot2d.h" */
  254.  
  255. void 
  256. plot_twod ()
  257. {
  258.     Point          *data;
  259.     int             i, j, tmp;
  260.     int             c_levels = 0, c_array[256];
  261.     char            buf[256], *str, *p;
  262.     bool            done = FALSE;
  263.  
  264.     int             xdim = curr_image.xdim;
  265.     int             ydim = curr_image.ydim;
  266.     int             xpos = curr_image.startx;
  267.     int             ypos = curr_image.starty;
  268.  
  269.     int        delta;
  270.     int        plevels = 10;
  271.     char        *spaces = "      ";
  272.  
  273.     clear_plot ();
  274.  
  275.     strcpy (buf, (char *) panel_get_value (level_item));
  276.     p = buf;
  277.     while (!done)
  278.     {
  279.         while (*p == ' ')
  280.             p++;
  281.         str = p;
  282.         while (*p != ' ' && *p != ',' && *p != '\0')
  283.             p++;
  284.         if (*p == '\0')
  285.             done = TRUE;
  286.         else
  287.             *p++ = '\0';
  288.  
  289.         if (str != p)
  290.         {
  291.             c_array[c_levels++] = (int) atoi (str);
  292.         }
  293.     }
  294.     if (c_levels == 0)
  295.         return;
  296.  
  297.     for (i = c_levels - 1; i > 0; i--)
  298.     {
  299.         for (j = 0; j < i; j++)
  300.         {
  301.             if (c_array[j] > c_array[j + 1])
  302.             {
  303.                 tmp = c_array[j];
  304.                 c_array[j] = c_array[j + 1];
  305.                 c_array[j + 1] = tmp;
  306.             }
  307.         }
  308.     }
  309.  
  310.     sprintf (msgstr, "Note: Contour levels - ");
  311.     msg_write(msgstr);
  312.     for (i = 0; i < c_levels; i += plevels)
  313.     {
  314.         strcpy (msgstr, spaces);
  315.         delta = (c_levels - i) < plevels ? c_levels - i : plevels;
  316.         for (j = i; j < i + delta; j++)
  317.         {
  318.             sprintf (wkstr, "%d ", c_array[j]);
  319.             strcat (msgstr, wkstr);
  320.         }
  321.         msg_write(msgstr);
  322.     }
  323.  
  324.     tmp = (xdim + 1) >> 1;
  325.     if ((data = (Point *) malloc (tmp * ydim * sizeof (Point))) == NULL)
  326.     {
  327.         msg_write ("Error: Not enough memory to hold plot data matrix.");
  328.         return;
  329.     }
  330.     if (get_matrix (data, xdim, ydim, xpos, ypos) != 0)
  331.         return;
  332.  
  333.     contour (tmp, ydim, data, c_levels, c_array);
  334.     free (data);
  335. }
  336. /* EOF */
  337.